<html>
<head><meta charset="utf-8"><title>Fully unwrap results · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html">Fully unwrap results</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="182674865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182674865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182674865">(Dec 05 2019 at 16:13)</a>:</h4>
<p>Is there a way to fully unwrap results ?e.g. if I build a <code>Result&lt;Result&lt;Result&lt;Result&lt;</code> in generic code, is there a macro or crate that allows <code>?</code> to unwrap them all at once returning the first error ?</p>



<a name="182674976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182674976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182674976">(Dec 05 2019 at 16:14)</a>:</h4>
<p>afaik, nothing existing</p>



<a name="182675001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182675001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182675001">(Dec 05 2019 at 16:14)</a>:</h4>
<p>I think it would be plausibly not too hard to implement, though not sure -- seems like either not too hard or pretty much impossible :)</p>



<a name="182675442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182675442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182675442">(Dec 05 2019 at 16:19)</a>:</h4>
<p>I don't think <code>?</code> would work, because it can only return one error type, right ?</p>



<a name="182675523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182675523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182675523">(Dec 05 2019 at 16:20)</a>:</h4>
<p>We would need to construct an enum of all error types in the chain, and return that as an error, to be able to use <code>?</code>.</p>



<a name="182675580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182675580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182675580">(Dec 05 2019 at 16:21)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> do you want <code>μ : T (T a) -&gt; T a</code> ?</p>



<a name="182675713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182675713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182675713">(Dec 05 2019 at 16:22)</a>:</h4>
<p>these "flatten maximally" are probably not doable without specialization</p>



<a name="182675849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182675849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182675849">(Dec 05 2019 at 16:24)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">C</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="nb">Result</span><span class="o">&lt;</span><span class="nb">Result</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">C</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">....</span>:
    <span class="nc">x</span><span class="o">?</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="182676022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676022">(Dec 05 2019 at 16:25)</a>:</h4>
<p>maybe you can use frunk for "structural coproducs"</p>



<a name="182676034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676034">(Dec 05 2019 at 16:25)</a>:</h4>
<p>to achieve <code>A | B | C</code></p>



<a name="182676166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676166">(Dec 05 2019 at 16:26)</a>:</h4>
<p>thanks, that solves half of the problem at least</p>



<a name="182676226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676226">(Dec 05 2019 at 16:27)</a>:</h4>
<p>I can use specialization to recurse through the <code>Result</code>s as well</p>



<a name="182676258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676258">(Dec 05 2019 at 16:27)</a>:</h4>
<p>and use that coproduct for the error type</p>



<a name="182676335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676335">(Dec 05 2019 at 16:28)</a>:</h4>
<p>but i don't think I can compute the type of the coproduct easily, I'd need to manually type it in</p>



<a name="182676815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676815">(Dec 05 2019 at 16:32)</a>:</h4>
<p>have a look at <a href="https://docs.rs/frunk/0.3.0/frunk/coproduct/index.html" target="_blank" title="https://docs.rs/frunk/0.3.0/frunk/coproduct/index.html">https://docs.rs/frunk/0.3.0/frunk/coproduct/index.html</a></p>



<a name="182676967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676967">(Dec 05 2019 at 16:34)</a>:</h4>
<p>yes, that's what I checked out</p>



<a name="182676995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182676995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182676995">(Dec 05 2019 at 16:34)</a>:</h4>
<p>Maybe if I get all my error types to implement a particular trait, I can use that to merge them.</p>



<a name="182690032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182690032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182690032">(Dec 05 2019 at 18:48)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> You could use <code>?</code> as long as your return error type is something that impls <code>From&lt;A&gt;</code>, <code>From&lt;B&gt;</code>, and <code>From&lt;C&gt;</code>.</p>



<a name="182690102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182690102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182690102">(Dec 05 2019 at 18:49)</a>:</h4>
<p>Wouldn't I need to use <code>???</code> ?</p>



<a name="182690122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182690122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182690122">(Dec 05 2019 at 18:49)</a>:</h4>
<p>Yes, that would be the compromise.</p>



<a name="182690402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182690402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182690402">(Dec 05 2019 at 18:52)</a>:</h4>
<p>Personally I've started to grow on using <code>snafu</code> for everything, which lets you express <code>???</code> with more context like the following</p>
<div class="codehilite"><pre><span></span><span class="n">result</span><span class="p">.</span><span class="n">context</span><span class="p">(</span><span class="n">error</span>::<span class="n">Http</span><span class="p">)</span><span class="o">?</span><span class="p">.</span><span class="n">context</span><span class="p">(</span><span class="n">error</span>::<span class="n">Database</span><span class="p">)</span><span class="o">?</span><span class="p">.</span><span class="n">context</span><span class="p">(</span><span class="n">error</span>::<span class="n">InvalidUserData</span><span class="p">)</span><span class="o">?</span><span class="w"></span>
</pre></div>



<a name="182724139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182724139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182724139">(Dec 06 2019 at 02:25)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span>  <a href="https://stackoverflow.com/questions/52453180/how-do-i-unwrap-an-arbitrary-number-of-nested-option-types" target="_blank" title="https://stackoverflow.com/questions/52453180/how-do-i-unwrap-an-arbitrary-number-of-nested-option-types">https://stackoverflow.com/questions/52453180/how-do-i-unwrap-an-arbitrary-number-of-nested-option-types</a></p>



<a name="182724181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182724181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182724181">(Dec 06 2019 at 02:26)</a>:</h4>
<p>Should be able to apply either answer to <code>Result</code></p>



<a name="182739215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182739215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182739215">(Dec 06 2019 at 08:33)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> Wouldn't you have to have <code>map_err</code> to use <code>and_then</code> to flatten it to a single type?</p>



<a name="182766262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Fully%20unwrap%20results/near/182766262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Fully.20unwrap.20results.html#182766262">(Dec 06 2019 at 14:40)</a>:</h4>
<p>Mmm, probably.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>